// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcMatrix3.h"
#include "MgcPointLight.h"

MgcImplementRTTI(MgcPointLight,MgcLight);
MgcImplementStream(MgcPointLight);

//---------------------------------------------------------------------------
MgcPointLight::MgcPointLight ()
    :
    m_kLocation(MgcVector3::ZERO)
{
}
//---------------------------------------------------------------------------
void MgcPointLight::ComputeDiffuse (const MgcMatrix3& rkWorldRotate,
    const MgcVector3& rkWorldTranslate, MgcReal fWorldScale,
    const MgcVector3* akVertex, const MgcVector3* akNormal,
    unsigned int uiQuantity, const bool* abVisible, MgcColor* akDiffuse)
{
    // transform light position to model space of old mesh
    MgcReal fInvWScale = 1.0/fWorldScale;
    MgcVector3 kDiff = m_kLocation - rkWorldTranslate;
    MgcVector3 kModelPos = (kDiff*rkWorldRotate)*fInvWScale;

    // adjust diffuse color by light intensity
    MgcColor kAdjDiffuse = m_fIntensity*m_kDiffuse;

    for (unsigned int uiV = 0; uiV < uiQuantity; uiV++)
    {
        if ( abVisible[uiV] )
        {
            kDiff = kModelPos - akVertex[uiV];
            MgcReal fDot = kDiff.Dot(akNormal[uiV]);
            if ( fDot > 0.0 )
            {
                fDot /= kDiff.Length();
                akDiffuse[uiV] += fDot*kAdjDiffuse;
            }
        }
    }
}
//---------------------------------------------------------------------------
void MgcPointLight::ComputeSpecular (const MgcMatrix3& rkWorldRotate,
    const MgcVector3& rkWorldTranslate, MgcReal fWorldScale,
    const MgcVector3* akVertex, const MgcVector3* akNormal,
    unsigned int uiQuantity, const bool* abVisible,
    const MgcVector3& rkCameraModelLocation, MgcColor* akSpecular)
{
    // transform light position to model space of old mesh
    MgcReal fInvWScale = 1.0/fWorldScale;
    MgcVector3 kDiff = m_kLocation - rkWorldTranslate;
    MgcVector3 kModelPos = (kDiff*rkWorldRotate)*fInvWScale;

    // adjust specular color by light intensity
    MgcColor kAdjSpecular = m_fIntensity*m_kSpecular;

    for (unsigned int uiV = 0; uiV < uiQuantity; uiV++)
    {
        if ( abVisible[uiV] )
        {
            kDiff = kModelPos - akVertex[uiV];
            MgcReal fDot = kDiff.Dot(akNormal[uiV]);
            MgcVector3 kReflect = (2.0*fDot)*akNormal[uiV] - kDiff;
            MgcVector3 kViewDir = rkCameraModelLocation - akVertex[uiV];
            fDot = kViewDir.Dot(kReflect);
            if ( fDot > 0.0 )
            {
                MgcReal fVSL = kViewDir.Dot(kViewDir);
                MgcReal fRSL = kReflect.Dot(kReflect);
                MgcReal fDenom = MgcMath::Sqrt(fVSL*fRSL);
                akSpecular[uiV] += (fDot/fDenom)*kAdjSpecular;
            }
        }
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcPointLight::Factory (MgcStream& rkStream)
{
    MgcPointLight* pkObject = new MgcPointLight;
    MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
    pkObject->Load(rkStream,pkLink);
    return pkObject;
}
//---------------------------------------------------------------------------
void MgcPointLight::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcLight::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_kLocation);
}
//---------------------------------------------------------------------------
void MgcPointLight::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcLight::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcPointLight::Register (MgcStream& rkStream)
{
    return MgcLight::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcPointLight::Save (MgcStream& rkStream)
{
    MgcLight::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_kLocation);
}
//---------------------------------------------------------------------------
